﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using HtmlAgilityPack;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;

namespace BAR_POS
{
    public partial class DrinkDatabaseScreen : DevExpress.XtraEditors.XtraUserControl
    {
        static string sourceString;
        string[] files;
        int i = 0;

        public DrinkDatabaseScreen()
        {
            InitializeComponent();

        }

        public static string GetTitle(string source)
        {
            const string removeString = "<title>";

            int index = source.IndexOf(removeString);
            int length = removeString.Length;
            String startOfString = source.Substring(0, index);
            String endOfString = source.Substring(index + length);
            //String cleanPath = startOfString + endOfString;

            string NewSourceString = endOfString;

            const string removeString2 = "</title>";

            int index2 = NewSourceString.IndexOf(removeString2);
            int length2 = removeString2.Length;
            String NewstartOfString = NewSourceString.Substring(0, index2);
            String NewendOfString = NewSourceString.Substring(index2 + length2);

            
            if (NewstartOfString.ToUpper().Contains("RECIPE"))
            {
                NewstartOfString = NewstartOfString.ToUpper().Replace("RECIPE", " ");
            }

            return StripTagsRegex(NewstartOfString.ToUpper());
        }

        public static string GetIngerients(string source)
        {

            const string removeString = "ingredients";

            int index = sourceString.IndexOf(removeString);
            int length = removeString.Length;
            String startOfString = sourceString.Substring(0, index);
            String endOfString = sourceString.Substring(index + length + 27);
            //String cleanPath = startOfString + endOfString;

            string NewSourceString = endOfString;

            const string removeString2 = "</div>";

            int index2 = NewSourceString.IndexOf(removeString2);
            int length2 = removeString2.Length;
            String NewstartOfString = NewSourceString.Substring(0, index2);
            String NewendOfString = NewSourceString.Substring(index2 + length2);

            return StripTagsRegex(NewstartOfString);
        }

        public static string GetDirections(string source)
        {
            const string removeString = "RecipeDirections instructions";

            int index = sourceString.IndexOf(removeString);
            int length = removeString.Length;
            String startOfString = sourceString.Substring(0, index);
            String endOfString = sourceString.Substring(index + length + 2);
            //String cleanPath = startOfString + endOfString;

            string NewSourceString = endOfString;

            const string removeString2 = "</div>";

            int index2 = NewSourceString.IndexOf(removeString2);
            int length2 = removeString2.Length;
            String NewstartOfString = NewSourceString.Substring(0, index2);
            String NewendOfString = NewSourceString.Substring(index2 + length2);

            return StripTagsRegex(NewstartOfString);
        }

        public static string StripTagsRegex(string source)
        {
            string Source = Regex.Replace(source, "<.*?>", string.Empty);

           return Regex.Replace(Source, "&reg;", string.Empty);
        }

        private void DrinkDatabaseScreen_Load(object sender, EventArgs e)
        {
            files = Directory.GetFiles("C:\\Downloaded Web Sites\\www.drinksmixer.com\\", "drink*.html");

            for (int x = 0; x < files.Length; x++)
            {
                comboBoxEdit1.Properties.Items.Add(x.ToString());
            }

            sourceString = File.ReadAllText(files[i]);

            labelControl1.Text = GetTitle(sourceString);
            memoEdit1.Text = GetIngerients(sourceString);
            memoEdit2.Text = GetDirections(sourceString);

            labelControl2.Text = "Working on # " + i.ToString() + " of " + files.Length.ToString();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            
        }

        private void comboBoxEdit1_SelectedValueChanged(object sender, EventArgs e)
        {
            sourceString = File.ReadAllText(files[Convert.ToInt32(comboBoxEdit1.SelectedText)]);

            labelControl1.Text = GetTitle(sourceString);
            memoEdit1.Text = GetIngerients(sourceString);
            memoEdit2.Text = GetDirections(sourceString);

            i = Convert.ToInt32(comboBoxEdit1.SelectedText);
            labelControl2.Text = "Working on # " + i.ToString() + " of " + files.Length.ToString();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow newReceipt = DataTableClass.BeverageDatabase.NewRow();//dataSet1.Tables["Customers"].NewRow();

                //newCustomersRow["EntryNo"] = lblReceiptNo.Text;
                newReceipt["Name"] = labelControl1.Text;
                newReceipt["Ingredients"] = memoEdit1.Text;
                newReceipt["Directions"] = memoEdit2.Text;

                DataTableClass.BeverageDatabase.Rows.Add(newReceipt);
            }
            catch (Exception E)
            {
                MessageBox.Show(E.Message);
            }

            i++;
            
            sourceString = File.ReadAllText(files[i]);

            labelControl1.Text = GetTitle(sourceString);
            memoEdit1.Text = GetIngerients(sourceString);
            memoEdit2.Text = GetDirections(sourceString);

            labelControl2.Text = "Working on # " + i.ToString() + " of " + files.Length.ToString();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            try
            {
                DataTableClass.SendDataToSqlServer(DataTableClass.BeverageDatabase.TableName);
            }
            catch (Exception E)
            {
                MessageBox.Show(E.Message);
            }
        }
    }
}
